/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package giuliani_mapelli_marocco.boundary;

import giuliani_mapelli_marocco.business.ImportExportManager;
import giuliani_mapelli_marocco.enumerations.InternalMessageType;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.xml.bind.JAXBException;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.UploadedFile;

@Named
@RequestScoped
public class ImportExportBean {
    
    @EJB
    private ImportExportManager iem;
        
    private UploadedFile fileImport;
    
    /**
     * This method will handle the import of the calendar using the file uploaded by user in the 
     * correct section
     * @return the next page to navigate to
     */
    public String importCalendar() {
        FacesMessage message;
        
        if(fileImport != null) {
            try {
                if(iem.importCalendar(fileImport.getInputstream()))
                    //if the import is successfull
                    message = new FacesMessage(FacesMessage.SEVERITY_INFO,InternalMessageType.IMPORT_SUCCESSFUL.getMessageContent(), fileImport.getFileName() + " is uploaded.");
                else {
                    //if the import is failed
                    message = new FacesMessage(FacesMessage.SEVERITY_ERROR, InternalMessageType.IMPORT_FAILED.getMessageContent(), "Time inconsistencies found with your calendar events");
                }
            } catch (IOException | JAXBException e) {
                message = new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "");
            }
        } else {
            message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "");
        }
        FacesContext.getCurrentInstance().addMessage(null, message);
        return null;
    }
    
    /**
     * This method will provide to call the method in the ImportExportManager to export actually the calendar
     * @return return the xml file if 
     */
    public StreamedContent exportCalendar(){
        StreamedContent file = null;
        FacesMessage message;
        ByteArrayOutputStream out;
        try {
            out = iem.exportCalendar();
            file = new DefaultStreamedContent(new ByteArrayInputStream(out.toByteArray()),"text/xml", "calendar.xml");
            message = new FacesMessage("Information", "File was successfully saved");
            FacesContext.getCurrentInstance().addMessage(null, message);
        } catch (JAXBException ex) {
            Logger.getLogger(ImportExportBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return file;
    }
    
      public UploadedFile getFile() {
        return fileImport;
    }

    public void setFile(UploadedFile file) {
        this.fileImport = file;
    }
    
}
